class Observe {
    constructor(data) {
        this.walk(data)
    }
    walk(data) {
        if (!data || typeof data !== 'object') {
            return
        }
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })
    }
    defineReactive(data, key, val) {
        this.walk(val)
        let dep = new Dep()
        Object.defineProperty(data, key, {
            configurable: true,
            enumerable: true,
            get() {
                // 收集依赖
                Dep.target && dep.addSub(Dep.target)
                return val
            },
            set: newVal => {
                if (newVal === val) {
                    return
                }
                
                this.walk(newVal)
                val = newVal
                // 发送通知
                dep.notify()
            }
        })
    }
}